package com.zhupf.stack;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author zhupf
 * @date 2024年02月05日 10:19
 * @Description
 *
 *  20. 有效的括号
 */
public class IsValid {

    public static Map<Character,Character> dix = new HashMap();

    public static void main(String[] args) {
        isValid("()");
    }

    public static boolean isValid(String s) {
        dix.put('(',')');
        dix.put('{','}');
        dix.put('[',']');
        dix.put('?','?');
        int length = s.length();
        Stack<Character> stack = new Stack<>();
        for (int i = length-1;i>=0;i--){
            char c = s.charAt(i);
            if(dix.containsKey(c)){
                if(stack.isEmpty() || !stack.peek().equals(dix.get(c))){
                    return false;
                }else {
                    stack.pop();
                }
            }else {
                stack.push(c);
            }
        }
        if(stack.isEmpty()){
            return true;
        }
        return false;
    }
}
